#!/usr/bin/python
# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
Generates some useful statistics about the dictionaries

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


#    $Id: extractStat.py 756 2012-02-27 04:28:24Z rrs $


import os
import re
import glob
import nltk
import collections
import cPickle
from math import *
from operator import itemgetter

from CXLReader import CXLReader
from phrase import Phrase



# Some statistical inference functions
def bin(n, i):
    '''Returns the Newton binomial (n i)'''
    return float(factorial(n))/(factorial(i)*factorial(n-i))


def binomial(n, i, pe):
    '''Returns the probability of the binomial distribution for i positives in n
       draws, where it is considered that a draw has a probability pe of
       success (1)
    '''
    return bin(n, i)*(pe**i)*((1-pe)**(n-i))


def pValue(n, i, pe):
    pv = 0
    for j in xrange(i, n+1):
        pv += binomial(n, j, pe)
    return pv


def grep(string,list):
    """Greps"""
    expr = re.compile(string)
    return filter(expr.search,list)


def countAppearances(pattern, content):
    passed = grep(pattern, content)
    return len(passed)


if __name__ == '__main__':
    basePath = r'/home/rrs/01 - Doutorado/05 - MP ITA/02 - Aquisicao/*/*.cxl'
    files = sorted(glob.glob(basePath))
    experimentNumber = re.compile(r'/(\d\d)/')

    print('Experiment\t'
          'Nodes\t'
          'Arcs\t'
          'Connections\t'
          'Avg Node degree\t'
          'Avg Node indegree\t'
          'Avg Node outdegree\t'
          'Avg Arc degree\t'
          'Avg Arc indegree\t'
          'Avg Arc outdegree')

    order = ['Nodes',
             'Arcs',
             'Connections',
             'Avg C degree',
             'Avg C indegree',
             'Avg C outdegree',
             'Avg LP degree',
             'Avg LP indegree',
             'Avg LP outdegree']
    for f in files:
        reader = CXLReader(f)
        reader.parse()
        graph = reader.graph
        
        statistics = graph.getStatistics()
        statisticsMessage = '\t'.join('%0.2f' % statistics[stat]
                                      for stat in order)
        experiment = experimentNumber.findall(f)
        experiment = experiment[0]
        print(experiment + '\t' + statisticsMessage)
        
        
